home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_064 / examples / screensave.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  10KB  |  392 lines

  1. /*
  2.  *  ScreenSave.c --  Carolyn Scheppner  CBM  10/86
  3.  *                   Saves front screen as ILBM file
  4.  *                   Saves a CAMG chunk for HAM, etc.
  5.  *                   Creates icon for ILBM file
  6.  *
  7.  *     Uses IFF rtns by J.Morrison and S.Shaw of Electronic Arts
  8.  *
  9.  *       (all C code including IFF modules compiled with -v on LC2)
  10.  * Linkage information:
  11.  * FROM     AStartup.obj, ScreenSave.o, iffw.o, ilbmw.o, packer.o
  12.  * TO       ScreenSave
  13.  * LIBRARY  Amiga.lib, LC.lib
  14.  *
  15.  */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <libraries/dos.h>
  20. #include <libraries/dosextens.h>
  21. #include <graphics/gfxbase.h>
  22. #include <graphics/rastport.h>
  23. #include <graphics/gfx.h>
  24. #include <graphics/view.h>
  25.  
  26. #include <intuition/intuition.h>
  27. #include <intuition/intuitionbase.h>
  28. #include <workbench/workbench.h>
  29. #include <workbench/startup.h>
  30.  
  31. #include <iff/ilbm.h>
  32.  
  33. /* From AStartup */
  34. extern  LONG  stdin, stdout, stderr;
  35.  
  36. /* CAMG Stuff */
  37. typedef struct {
  38.    ULONG ViewModes;
  39.    } CamgChunk;
  40.  
  41. #define PutCAMG(context, camg)  \
  42.     PutCk(context, ID_CAMG, sizeof(CamgChunk),(BYTE *)camg)
  43.  
  44. #define bufSize 512
  45.  
  46. /* Other Stuff */
  47. struct IntuitionBase *IntuitionBase;
  48. struct GfxBase       *GfxBase;
  49. ULONG  IconBase;
  50.  
  51. struct Screen   *frontScreen;
  52.  
  53. struct ViewPort *picViewPort;
  54. struct BitMap   *picBitMap;
  55. WORD            *picColorTable;
  56. ULONG            picViewModes;
  57. BOOL fromWB, newStdio;
  58.  
  59. #define INBUFSZ 40
  60. char sbuf[INBUFSZ];
  61. char nbuf[INBUFSZ];
  62.  
  63. char conSpec[] = "CON:0/40/639/160/ ScreenSave ";
  64.  
  65. /* Definitions for ILBM Icon */
  66. USHORT  ILBMimagedata[] = {
  67.  0xFFFF, 0xFFFC,
  68.  0xC000, 0x000C,
  69.  0xC000, 0x000C,
  70.  0xC1E7, 0x9E0C,
  71.  0xC1F8, 0x7E0C,
  72.  0xC078, 0x780C,
  73.  0xC187, 0x860C,
  74.  0xC078, 0x780C,
  75.  0xC1F8, 0x7E0C,
  76.  0xC1E7, 0x9E0C,
  77.  0xC000, 0x000C,
  78.  0xC000, 0x000C,
  79.  0xFFFF, 0xFFFC,
  80.  0x0000, 0x0000,
  81.  0x0000, 0x0000,
  82. /**/
  83.  0xFFFF, 0xFFFC,
  84.  0xFFFF, 0xFFFC,
  85.  0xF800, 0x007C,
  86.  0xF9E0, 0x1E7C,
  87.  0xF980, 0x067C,
  88.  0xF807, 0x807C,
  89.  0xF81F, 0xE07C,
  90.  0xF807, 0x807C,
  91.  0xF980, 0x067C,
  92.  0xF9E0, 0x1E7C,
  93.  0xF800, 0x007C,
  94.  0xFFFF, 0xFFFC,
  95.  0xFFFF, 0xFFFC,
  96.  0x0000, 0x0000,
  97.  0x0000, 0x0000,
  98. /**/
  99.  };
  100.  
  101. struct Image ILBMimage = {
  102.    0,0,                     /* Leftedge, Topedge */
  103.    30,15,                   /* Width Height */
  104.    2,                       /* Depth */
  105.    &ILBMimagedata[0],       /* Data for image */
  106.    3,0                      /* PlanePick, PlaneOnOff */
  107.    };
  108.  
  109. struct DiskObject ILBMobject = {
  110.    WB_DISKMAGIC,
  111.    WB_DISKVERSION,
  112.  
  113.    /* Gadget Structure */
  114.    NULL,                    /* Ptr to next gadget */
  115.    0,0,                     /* Leftedge, Topedge */
  116.    30,15,                   /* Width, Height */
  117.    GADGHBOX|GADGIMAGE,      /* Flags */
  118.    RELVERIFY|GADGIMMEDIATE, /* Activation */
  119.    BOOLGADGET,              /* Type */
  120.    (APTR)&ILBMimage,        /* Render */
  121.    NULL,                    /* Select Render */
  122.    NULL,                    /* Text */
  123.    NULL,NULL,NULL,NULL,     /* Exclude, Special, ID, UserData */
  124.  
  125.    4,                       /* WBObject type */
  126.    ":Display",              /* Default tool */
  127.    NULL,                    /* Tool Types */
  128.    NO_ICON_POSITION,        /* Current X */
  129.    NO_ICON_POSITION,        /* Current Y */
  130.    NULL,NULL,NULL,          /* Drawer, ToolWindow, Stack */
  131.    };
  132.  
  133.  
  134. main(argc, argv)
  135. int argc;
  136. char **argv;
  137.    {
  138.    LONG            file;
  139.    IFFP            iffp = NO_FILE;
  140.    char            *filename;
  141.    int l;
  142.  
  143.    newStdio = FALSE;
  144.    fromWB = (argc==0) ? TRUE : FALSE;
  145.  
  146.    if((fromWB) && (!(newStdio = openStdio(&conSpec[0]))))
  147.       {
  148.       return(0);
  149.       }
  150.  
  151.    if ((IntuitionBase =
  152.       (struct IntuitionBase *)OpenLibrary("intuition.library",0))==NULL)
  153.          cleanexit("Can't open intuition.library\n");
  154.  
  155.    if ((GfxBase =
  156.       (struct GfxBase *)OpenLibrary("graphics.library",0))==NULL)
  157.       cleanexit("Can't open graphics.library\n");
  158.  
  159.    if ((IconBase = OpenLibrary("icon.library",0))==NULL )
  160.       cleanexit("Can't open icon.library\n");
  161.  
  162.    printf("ScreenSave --- C. Scheppner  CBM  10/86\n");
  163.    printf("   Saves the front screen as an IFF ILBM file\n");
  164.    printf("   A CAMG chunk is saved (for HAM pics, etc.)\n\n");
  165.  
  166.    if(argc>1)                 /* Passed filename via command line  */
  167.       {
  168.       filename = argv[1];
  169.       }
  170.    else
  171.       {
  172.       printf("Enter filename for save: ");
  173.       l = gets(&nbuf[0]);
  174.  
  175.       if(l==0)                /* No filename - Exit */
  176.          {
  177.          cleanexit("\nScreen not saved, filename required\n");
  178.          }
  179.       else
  180.          {
  181.          filename = &nbuf[0];
  182.          }
  183.       }
  184.  
  185.    if (!(file = Open(filename, MODE_NEWFILE)))
  186.       cleanexit("Can't open output file\n");
  187.      
  188.    Write(file,"x",1);  /* 1.1 so Seek to beginning works ? */
  189.  
  190.    printf("Click here and press <RETURN> when ready: ");
  191.    gets(&sbuf[0]);
  192.    printf("Front screen will be saved in 10 seconds\n");
  193.    Delay(500);
  194.  
  195.    Forbid();
  196.    frontScreen  = IntuitionBase->FirstScreen;
  197.    Permit();
  198.  
  199.    picViewPort = &( frontScreen->ViewPort );
  200.    picBitMap = (struct BitMap*)picViewPort->RasInfo->BitMap;
  201.    picColorTable = (WORD *)picViewPort->ColorMap->ColorTable;
  202.    picViewModes = (ULONG)picViewPort->Modes;
  203.  
  204.    printf("\nSaving...\n");
  205.  
  206.    iffp = PutPicture(file, picBitMap, picColorTable, picViewModes);
  207.    Close(file);
  208.  
  209.    if (iffp == IFF_OKAY)
  210.       {
  211.       printf("Screen saved\n");
  212.       if(!(PutDiskObject(filename,&ILBMobject)))
  213.          {
  214.          cleanexit("Error saving icon\n");
  215.          }
  216.       printf("Icon saved\n");
  217.       }
  218.    cleanexit("Done\n");
  219.    }
  220.  
  221.  
  222. cleanexit(s)
  223.    char  *s;
  224.    {
  225.    if(*s) printf(s);
  226.    if ((fromWB)&&(*s))    /* Wait so user can read messages */
  227.       {
  228.       printf("\nPRESS RETURN TO EXIT\n");
  229.       gets(&sbuf[0]);
  230.       }
  231.    cleanup();
  232.    exit();
  233.    }
  234.  
  235. cleanup()
  236.    {
  237.    if (newStdio)  closeStdio();
  238.    if (GfxBase) CloseLibrary(GfxBase);
  239.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  240.    if (IconBase) CloseLibrary(IconBase);
  241.    }
  242.  
  243.  
  244. openStdio(conspec)
  245. char *conspec;
  246.    {
  247.    LONG wfile;
  248.    struct Process *proc;
  249.    struct FileHandle *handle;
  250.  
  251.    if (!(wfile = Open(conspec,MODE_NEWFILE)))  return(0);
  252.    stdin  = wfile;
  253.    stdout = wfile;
  254.    stderr = wfile;
  255.    handle = (struct FileHandle *)(wfile << 2);
  256.    proc = (struct Process *)FindTask(NULL);
  257.    proc->pr_ConsoleTask = (APTR)(handle->fh_Type);
  258.    proc->pr_CIS = (BPTR)stdin;
  259.    proc->pr_COS = (BPTR)stdout;
  260.    return(1);
  261.    }
  262.  
  263. closeStdio()
  264.    {
  265.    struct Process *proc;
  266.    struct FileHandle *handle;
  267.  
  268.    if (stdin > 0)  Close(stdin);
  269.    stdin  = -1;
  270.    stdout = -1;
  271.    stderr = -1;
  272.    handle = (struct FileHandle *)(stdin << 2);
  273.    proc = (struct Process *)FindTask(NULL);
  274.    proc->pr_ConsoleTask = NULL;
  275.    proc->pr_CIS = NULL;
  276.    proc->pr_COS = NULL;
  277.    }
  278.  
  279.  
  280. gets(s)
  281. char *s;
  282.    {
  283.    int l = 0, max = INBUFSZ - 1;
  284.  
  285.    while (((*s = getchar()) !='\n' )&&(l < max)) s++, l++;
  286.    *s = NULL;
  287.    return(l);
  288.    }
  289.  
  290.  
  291. /* String Functions */
  292.  
  293. strlen(s)
  294. char *s;
  295.    {
  296.    int i = 0;
  297.    while(*s++) i++;
  298.    return(i);
  299.    }
  300.  
  301. strcpy(to,from)
  302. char *to, *from;
  303.    {
  304.    do
  305.       {
  306.       *to++ = *from;
  307.       }
  308.    while(*from++);
  309.    }
  310.  
  311.  
  312. /** PutPicture() ***********************************************************
  313.  *
  314.  * Put a picture into an IFF file.
  315.  * This procedure calls PutAnILBM, passing in an <x, y> location of <0, 0>,
  316.  * a NULL mask, and a locally-allocated buffer. It also assumes you want to
  317.  * write out all the bitplanes in the BitMap.
  318.  *
  319.  ***************************************************************************/
  320. Point2D nullPoint = {0, 0};
  321.  
  322. IFFP PutPicture(file, bitmap, colorMap, viewmodes)
  323.       LONG file;  struct BitMap *bitmap;
  324.       WORD *colorMap;  ULONG viewmodes;
  325.    {
  326.    BYTE buffer[bufSize];
  327.    return( PutAnILBM(file, bitmap, NULL,
  328.            colorMap, bitmap->Depth, viewmodes,
  329.            &nullPoint, buffer, bufSize) );
  330.    }    
  331.  
  332.    
  333. /** PutAnILBM() ************************************************************
  334.  *
  335.  * Write an entire BitMap as a FORM ILBM in an IFF file.
  336.  * This version works for any display mode (C. Scheppner).
  337.  *
  338.  * Normal return result is IFF_OKAY.
  339.  *
  340.  * The utility program IFFCheck would print the following outline of the
  341.  * resulting file:
  342.  *
  343.  *   FORM ILBM
  344.  *     BMHD
  345.  *     CAMG
  346.  *     CMAP
  347.  *     BODY       (compressed)
  348.  *
  349.  ***************************************************************************/
  350. #define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}
  351.  
  352. IFFP PutAnILBM(file, bitmap, mask, colorMap, depth,
  353.                                 viewmodes, xy, buffer, bufsize)
  354.       LONG file;
  355.       struct BitMap *bitmap;
  356.       BYTE *mask;  WORD *colorMap; UBYTE depth;
  357.       ULONG viewmodes;
  358.       Point2D *xy; BYTE *buffer;  LONG bufsize;
  359.    {
  360.    BitMapHeader bmHdr;
  361.    CamgChunk    camgChunk;
  362.    GroupContext fileContext, formContext;
  363.    IFFP ifferr;
  364.    WORD pageWidth, pageHeight;
  365.  
  366.    pageWidth  = (bitmap->BytesPerRow) << 3;
  367.    pageHeight = bitmap->Rows;
  368.  
  369.    ifferr = InitBMHdr(&bmHdr, bitmap, mskNone,
  370.                       cmpByteRun1, 0, pageWidth, pageHeight);
  371.    /* You could write an uncompressed image by passing cmpNone instead
  372.     * of cmpByteRun1 to InitBMHdr. */
  373.    bmHdr.nPlanes = depth;   /* This must be  <= bitmap->Depth */
  374.    if (mask != NULL) bmHdr.masking = mskHasMask;
  375.    bmHdr.x = xy->x;   bmHdr.y = xy->y;
  376.  
  377.    camgChunk.ViewModes = viewmodes;
  378.  
  379.    CkErr( OpenWIFF(file, &fileContext, szNotYetKnown) );
  380.    CkErr(StartWGroup(&fileContext, FORM, szNotYetKnown, ID_ILBM, &formContext));
  381.  
  382.    CkErr( PutBMHD(&formContext, &bmHdr) );
  383.    CkErr( PutCAMG(&formContext, &camgChunk) );
  384.    CkErr( PutCMAP(&formContext, colorMap, depth) );
  385.    CkErr( PutBODY(&formContext, bitmap, mask, &bmHdr, buffer, bufsize) );
  386.  
  387.    CkErr( EndWGroup(&formContext) );
  388.    CkErr( CloseWGroup(&fileContext) );
  389.    return( ifferr );
  390.    }
  391.    
  392.